package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Plan;
import cn.wolfcode.crm.mapper.PlanMapper;
import cn.wolfcode.crm.query.impl.PlanQuery;
import cn.wolfcode.crm.service.IPlanService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class PlanServiceImpl implements IPlanService {
    @Autowired
    PlanMapper planMapper;

    public void saveOrUpdate(Plan record) {
        if (record.getId() == null) {
            record.setState(false);
            record.setCreate(UserContext.getCurrentUser());
            record.setSn(UserContext.getRandom(9));
            planMapper.insert(record);
        }else {
            planMapper.updateByPrimaryKey(record);
        }
    }

    public void cussaveOrUpdate(Plan record) {
        if (record.getId() == null) {
            record.setState(true);
            record.setCreate(UserContext.getCurrentUser());
            record.setSn(UserContext.getRandom(9));
            planMapper.insert(record);
        }else {
            planMapper.updateByPrimaryKey(record);
        }

    }

    public Plan get(Long id) {
        return planMapper.selectByPrimaryKey(id);
    }
    public PageResult query(PlanQuery qo) {
        int rows = planMapper.queryForCount(qo);
        if (rows == 0) {
            new PageResult(rows, Collections.EMPTY_LIST);
        }
        List<Plan> result = planMapper.queryForList(qo);
        return new PageResult(rows, result);
    }

    public List<Plan> selectAll() {
        return planMapper.selectAll();

    }

    public void delete(Long id) {
        planMapper.deleteByPrimaryKey(id);
    }

    public void setflag(Plan plan){
        planMapper.setflag(plan);
    }
}
